package cn.iocoder.yudao.module.pay.controller.admin.walletwithdraw;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pay.controller.admin.walletwithdraw.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.walletwithdraw.PayWalletWithdrawDO;
import cn.iocoder.yudao.module.pay.service.walletwithdraw.PayWalletWithdrawService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 钱包提现")
@RestController
@RequestMapping("/pay/wallet-withdraw")
@Validated
public class PayWalletWithdrawController {

    @Resource
    private PayWalletWithdrawService walletWithdrawService;

    @PostMapping("/create")
    @Operation(summary = "创建钱包提现")
    @PreAuthorize("@ss.hasPermission('pay:wallet-withdraw:create')")
    public CommonResult<Long> createWalletWithdraw(@Valid @RequestBody PayWalletWithdrawSaveReqVO createReqVO) {
        return success(walletWithdrawService.createWalletWithdraw(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新钱包提现")
    @PreAuthorize("@ss.hasPermission('pay:wallet-withdraw:update')")
    public CommonResult<Boolean> updateWalletWithdraw(@Valid @RequestBody PayWalletWithdrawSaveReqVO updateReqVO) {
        walletWithdrawService.updateWalletWithdraw(updateReqVO);
        return success(true);
    }
    @PutMapping("/check-pay")
    @Operation(summary = "审核钱包提现")
    @PreAuthorize("@ss.hasPermission('pay:wallet-withdraw:update')")
    public CommonResult<Boolean> updateWalletWithdraw(@Valid @RequestBody PayWalletWithdrawCheckReqVO updateReqVO) {
        walletWithdrawService.checkWalletWithdraw(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除钱包提现")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:wallet-withdraw:delete')")
    public CommonResult<Boolean> deleteWalletWithdraw(@RequestParam("id") Long id) {
        walletWithdrawService.deleteWalletWithdraw(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得钱包提现")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:wallet-withdraw:query')")
    public CommonResult<PayWalletWithdrawRespVO> getWalletWithdraw(@RequestParam("id") Long id) {
        PayWalletWithdrawDO walletWithdraw = walletWithdrawService.getWalletWithdraw(id);
        return success(BeanUtils.toBean(walletWithdraw, PayWalletWithdrawRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得钱包提现分页")
    @PreAuthorize("@ss.hasPermission('pay:wallet-withdraw:query')")
    public CommonResult<PageResult<PayWalletWithdrawRespVO>> getWalletWithdrawPage(@Valid PayWalletWithdrawPageReqVO pageReqVO) {
        PageResult<PayWalletWithdrawDO> pageResult = walletWithdrawService.getWalletWithdrawPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PayWalletWithdrawRespVO.class));
    }

    @GetMapping("/pickup-page")
    @Operation(summary = "获得钱包提现分页")
    @PreAuthorize("@ss.hasPermission('pay:wallet-withdraw:query')")
    public CommonResult<PageResult<PayWalletMetalWithdrawRespVO>> getWalletWithdrawPickupPage(@Valid PayWalletWithdrawPageReqVO pageReqVO) {
        PageResult<PayWalletMetalWithdrawRespVO> pageResult = walletWithdrawService.getWalletWithdrawPickupPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出钱包提现 Excel")
    @PreAuthorize("@ss.hasPermission('pay:wallet-withdraw:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWalletWithdrawExcel(@Valid PayWalletWithdrawPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayWalletWithdrawDO> list = walletWithdrawService.getWalletWithdrawPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "钱包提现.xls", "数据", PayWalletWithdrawRespVO.class,
                        BeanUtils.toBean(list, PayWalletWithdrawRespVO.class));
    }

}